package com.product.android.ui.activity;

import uk.co.senab.photoview.PhotoView;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.common.android.utils.ToastUtils;
import com.nd.android.u.allcommon.R;
import com.nostra13.universalimageloader.cache.disc.impl.TotalSizeLimitedDiscCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.product.android.business.headImage.HeadImageLoader;
import com.product.android.ui.activity.BaseReceiveActivity;
import com.product.android.ui.imageHolder.CircleProgress;
import com.product.android.ui.imageHolder.PictureHolder;
import com.product.android.utils.SaveTweetImageTask;

public class ShowImageActivity extends BaseReceiveActivity {

	protected String url;
//	protected File mFile;
//	protected Uri mImageUri;
	private PictureHolder mHolder;
	private int screenWidth = 800;

	public DisplayImageOptions mUserShowHeadOptions = null;
	private final String HEAD_DISC_CACHE_DIR = "head";
	private final int DISC_CACHE_SIZE_64MB = 64 * 1024 * 1024;
	private boolean mbUseThumbFirst = false;
	private Bitmap mbmThump;
    private Boolean isNeedToSave =null;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Bundle bundle = this.getIntent().getExtras();
		if (bundle.containsKey("url")) {
			url = bundle.getString("url");
		}
		if(bundle.containsKey("MINE")){
			isNeedToSave = bundle.getBoolean("MINE");
		}
		if(bundle.containsKey("optionType")){
			int optionType = bundle.getInt("optionType");
			if(optionType == 0 || optionType == 1){
				mUserShowHeadOptions = HeadImageLoader.mFaceDisplayOptions;
			}else if(optionType == 2){
				mUserShowHeadOptions = HeadImageLoader.mShowDisplayOptions;
			}
		}
		if(mUserShowHeadOptions == null){
			mUserShowHeadOptions = new DisplayImageOptions.Builder()
					.showStubImage(R.drawable.start)
					.showImageForEmptyUri(R.drawable.start)
					.showImageOnFail(R.drawable.start)
					.cacheOnDisc()
					.discCache(
							new TotalSizeLimitedDiscCache(StorageUtils
									.getIndividualCacheDirectory(ShowImageActivity.this,
											HEAD_DISC_CACHE_DIR),
									DISC_CACHE_SIZE_64MB)).build();
		}
		mbUseThumbFirst = bundle.getBoolean("bUseThumbFirst", false);
		if (mbUseThumbFirst) {
			mbmThump = bundle.getParcelable("thumb");
		}

		WindowManager manage = getWindowManager();
		if (manage != null) {
			Display display = manage.getDefaultDisplay();
			if (display != null) {
				screenWidth = display.getWidth();
			}
		}
		initComponent();
	}

	protected void onStart() {
		super.onStart();
		initComponentValue();
	}

	protected void initComponent() {
		mHolder = new PictureHolder();
		setContentView(R.layout.fragment_picture_flow_item);
		mHolder.rlProgress = (RelativeLayout) findViewById(R.id.rlProgress);
		mHolder.cpProgress = (CircleProgress) findViewById(R.id.cpProgress);
		mHolder.ivPicture = (PhotoView) findViewById(R.id.ivPicture);
		mHolder.leftBtn = (Button) findViewById(R.id.leftBtn);
		mHolder.leftBtn.setCompoundDrawablesWithIntrinsicBounds(R.drawable.bottom_left_save, 0, 0, 0);
		mHolder.rightBtn = (Button) findViewById(R.id.rightBtn);
		mHolder.rightBtn.setVisibility(View.GONE);
		mHolder.leftBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				doSave();
			}
		});
		if(isNeedToSave!=null){
			mHolder.leftBtn.setVisibility(View.GONE);
		}
		
	}

	protected void initComponentValue() {
		if(mbUseThumbFirst && null != mbmThump){
			mHolder.ivPicture.setImageBitmap(mbmThump);
			return;
		}
		ImageLoader.getInstance().displayImage(url, mHolder.ivPicture,mUserShowHeadOptions,
				new SimpleImageLoadingListener() {
					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {
						mHolder.progressListener.success(imageUri);
					}

					@Override
					public void onLoadingProgress(long totalSize,
							long downloadSize) {
						mHolder.progressListener.showProgress(totalSize, downloadSize,url);
					}

					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason) {
						mHolder.progressListener.fail(imageUri);
					}

				});

		// 设置图片
//		 SimpleImageLoader.commonDisplay(mHolder.ivPicture, url, screenWidth,
//		 mbUseThumbFirst, mbmThump, new UploadToMoreUserCallbackImpl());
	}

	private void doSave() {
		//如果sd卡不存在，不进行保存
		if (mHolder.isDownloading()){
			//正在下载，不保存
			ToastUtils.display(R.string.save_error_downloading);
			return;
		}
		if(!Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)){
			ToastUtils.display(this,
					R.string.no_sdcard_for_download);
			return;
		}
		//modify by chenqsh 2014-1-21
		new SaveTweetImageTask(url,null,this,mUserShowHeadOptions).execute();
	}

	@Override
	public void onResume() {
		mHolder.registerEvent();
		super.onResume();
	}

	@Override
	protected void onPause() {
		mHolder.unRegisterEvent();
		super.onPause();
	}

//	class UploadToMoreUserCallbackImpl implements UploadDownloadCallback {
//		@Override
//		public void uploadCallback(final int result, final String fileName,
//				final long fileSize, final String fkey, final String url) {
//		}
//
//		@Override
//		public void downloadCallback(int result, String fileName,
//				long fileSize, String fkey, String url) {
//			if (0 == result) {
//				// 成功
//				Bitmap bmp = ChatConfiguration.mImageManager.getFromLocal(url,
//						screenWidth);
//				if (bmp != null) {
//					mHolder.listener.success();
//					mHolder.ivPicture.setImageBitmap(bmp);
//				}
//			} else {
//				mHolder.listener.fail();
//			}
//		}
//
//		@Override
//		public void progressCallback(long now, long total) {
//			mHolder.listener.showProgress(total, now);
//		}
//	}
}